﻿using System;
using System.Diagnostics;
using FluentParser.Expressions;

namespace FluentParser.Expressions.Rules.Decoration
{
    /// <summary>
    /// This token is used exclusively when you need recursion. It allows to
    /// sets the token's behavior later.
    /// </summary>
    /// <typeparam name="TResult">Type of the result</typeparam>
    /// <example><code>
    /// var textWithParenthesis = Future&lt;string&gt;();
    /// var parseText = ParseString.Regex("[A-Za-z]+");
    /// textWithParenthesis.Source = textWithParenthesis.Surround("(", ")") | parseText;
    /// 
    /// textWithParenthesis.Parse("SomeWord")           --> "SomeWord"
    /// textWithParenthesis.Parse("((SomeWord))")       --> "SomeWord"
    /// textWithParenthesis.Parse("(((((SomeWord)))))") --> "SomeWord"
    /// </code></example>
    [DebuggerDisplay("[FutureRule Source={Source}")]
    public class FutureRule<TResult> : Expr<TResult> 
    {
        public Expr<TResult> Source { get; set; }

        public FutureRule()
        {
            TypeOperation = OperationType.Structural;
        }

        public override ParsingResult<TResult> Parse(GrammarContext context, int index)
        {
            return Source.Parse(context, index);
        }

        public override void ParseNode(GrammarContext context, ParsingNodeResult node)
        {
            ParsingNodeResult childNode = node.AddChild(Source, context);
            node.Value = childNode.Value;
            node.IsSuccess = childNode.IsSuccess;
        }
    }
}